#include"serialize/skill.h"

//skill definition
Skill::Skill()
{
    id = 0;
    description = "";
    attack_addon = 0;
    defense_addon = 0;
    strength_addon = 0;
    country = 0;
    country_addon = 0;
    opponent = 0;
    opponent_addon = 0;
    skill_condition_count = 0;
    condition = 0;
}

Skill::Skill(    qint32 _id,
                 QString _description,
                 qint8 _attack_addon,
                 qint8 _defense_addon,
                 qint8 _strength_addon,
                 qint8 _country,
                 qint8 _country_addon,
                 qint8 _opponent,
                 qint8 _opponent_addon,
                 qint32 _skill_condition_count,
                 SkillCondition* _condition)
{
   id = _id;
   description = _description;
   attack_addon = _attack_addon;
   defense_addon = _defense_addon;
   strength_addon = _strength_addon;
   country = _country;
   country_addon = _country_addon;
   opponent = _opponent;
   opponent_addon = _opponent_addon;
   skill_condition_count = _skill_condition_count;
   if(condition!=0)
       delete[] condition;
   for(qint32 i=0;i<skill_condition_count;i++)
       condition[i] = _condition[i];
}

Skill::Skill(const Skill& s)
{
    id = s.id;
    description = s.description;
    attack_addon = s.attack_addon;
    defense_addon = s.defense_addon;
    strength_addon = s.strength_addon;
    country = s.country;
    country_addon = s.country_addon;
    opponent = s.opponent;
    opponent_addon = s.opponent_addon;
    skill_condition_count = s.skill_condition_count;
    if(condition!=0)
        delete[] condition;
    for(qint32 i=0;i<skill_condition_count;i++)
        condition[i] = s.condition[i];
}

Skill& Skill::operator=(const Skill& s)
{
    if(&s == this)
        return *this;

    id = s.id;
    description = s.description;
    attack_addon = s.attack_addon;
    defense_addon = s.defense_addon;
    strength_addon = s.strength_addon;
    country = s.country;
    country_addon = s.country_addon;
    opponent = s.opponent;
    opponent_addon = s.opponent_addon;
    skill_condition_count = s.skill_condition_count;
    if(condition!=0)
        delete[] condition;
    for(qint32 i=0;i<skill_condition_count;i++)
        condition[i] = s.condition[i];
    return *this;
}

Skill::~Skill()
{
    if(condition!=0)
        delete[] condition;
}

ostream& operator <<(ostream& os,Skill& s)
{
    os<<"******start******"<<endl;
    os<<"Skill info:"<<endl;

    os<<"id:"<<(int)s.id <<endl;
    os<<"description:"<<s.description.toStdString()<<endl;
    os<<"attack_addon:"<<(int)s.attack_addon <<endl;
    os<<"defense_addon:"<<(int)s.defense_addon <<endl;
    os<<"strength_addon:"<<(int)s.strength_addon <<endl;
    os<<"country:"<<(int)s.country <<endl;
    os<<"country_addon:"<<(int)s.country_addon <<endl;
    os<<"opponent:"<<(int)s.opponent <<endl;
    os<<"opponent_addon:"<<(int)s.opponent_addon <<endl;
    os<<"skill_condition_count:"<<(int)s.skill_condition_count<<",They are:";
    for(qint32 i=0;i<s.skill_condition_count;i++)
    {
        os<<"("<<s.condition->id<<","<<s.condition->type<<")";
        if(i!=s.skill_condition_count-1)
            os<<",";
    }
    os<<endl;
    os<<"******end******"<<endl;

    return os;
}

QDataStream& operator<<(QDataStream& ds,Skill& s)
{
    ds<<s.id;
    ds<<s.description.toUtf8().data();
    ds<<s.attack_addon;
    ds<<s.defense_addon;
    ds<<s.strength_addon;
    ds<<s.country;
    ds<<s.country_addon;
    ds<<s.opponent;
    ds<<s.opponent_addon;
    ds<<s.skill_condition_count;

    for(qint32 i=0;i<s.skill_condition_count;i++)
    {
        ds<<s.condition[i].id;
        ds<<s.condition[i].type;
    }
    return ds;
}

QDataStream& operator>>(QDataStream& ds,Skill& s)
{
    char* temp;

    ds>>s.id;
    ds>>temp;
    s.description = QString::fromUtf8(temp);
    delete[] temp;
    ds>>s.attack_addon;
    ds>>s.defense_addon;
    ds>>s.strength_addon;
    ds>>s.country;
    ds>>s.country_addon;
    ds>>s.opponent;
    ds>>s.opponent_addon;
    ds>>s.skill_condition_count;

    if(s.condition!=0)
        delete[] s.condition;
    s.condition = new SkillCondition[s.skill_condition_count];
    for(qint32 i=0;i<s.skill_condition_count;i++)
    {
        ds>>s.condition[i].id;
        ds>>s.condition[i].type;
    }

    return ds;
}

